package com.mydemo.admin.service.impl;

import com.mydemo.admin.mapper.SysUserMapper;
import com.mydemo.admin.service.ISysRoleService;
import com.mydemo.common.Constant;
import com.mydemo.common.exception.user.UserException;
import com.mydemo.common.pojo.*;
import com.mydemo.admin.service.ISysUserService;
import com.mydemo.common.utils.RedisUtils;
import com.mydemo.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Last_Island
 * @version 1.0
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return
     */
    @Override
    public SysUser getUserByUsername(String username) {
        return sysUserMapper.getUserByUsername(username);
    }

    /**
     * 用户名是否重复
     *
     * @param username 用户名
     * @return 用户数量
     */
    @Override
    public boolean usernameNoRepeat(String username) {
        return sysUserMapper.countUserByUsername(username) == 0;
    }

    /**
     * 根据用户名获取登录用户信息
     *
     * @param username 用户名
     * @return
     */
    @Override
    public LoginUser getLoginUserByUsername(String username) {
        SysUser user = getUserByUsername(username);
        if (user == null) {
            throw new UserException("找不到用户");
        }
        List<Role> role = sysRoleService.getRoleByRoleId(user.getRoleId());
        return new LoginUser(user, role);
    }

    /**
     * 刷新缓存中的用户信息(当更改用户后调用此方法)
     *
     * @param username 用户名
     */
    public void refreshCacheLoginUser(String username) {
        String key = Constant.SYS_LOGIN_USER_KEY + username;
        if (redisUtils.getCacheObject(key) != null) {
            getLoginUserByUsername(username);
        }
    }

    /**
     * 获取用户列表
     *
     * @param sysUser 用户信息
     * @return
     */
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        return sysUserMapper.getList(sysUser);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param id 用户id
     * @return
     */
    @Override
    public RespResult getUserById(Long id) {
        SysUser sysUser = sysUserMapper.getUserById(id);
        if (sysUser == null) {
            return RespResult.error(403, "找不到用户");
        }
        return RespResult.success("获取用户信息成功", sysUser);
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean addUser(SysUser sysUser) {
        if (sysUserMapper.getUserByUsername(sysUser.getUsername()) != null) {
            throw new UserException("用户已存在");
        }
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        return sysUserMapper.addUser(sysUser) > 0;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public boolean delUser(Long id) {
        return sysUserMapper.delUser(id) > 0;
    }

    /**
     * 修改用户信息
     * 此方法不修改用户名和密码
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean updateUserInfo(SysUser sysUser) {
        sysUser.setUsername(null);
        sysUser.setPassword(null);
        return sysUserMapper.updateUser(sysUser) > 0;
    }
}
